//
//  HAPIManager.m
//  Project
//
//  Created by 黄振 on 16/8/28.
//  Copyright © 2016年 黄振. All rights reserved.
//

#import "HAPIManager.h"
#import <AFHTTPRequestOperationManager+RACSupport.h>

NSString *const MAPIManagerErrorDomain = @"MAPIManagerErrorDomain";
NSInteger const MAPIErrorNetworkError = -1009;
/* APIModule */
NSString *const APIModuleHome = @"hr/";
NSString *const APIModule = @"app/";
NSString *const APIModuleUpfile =@"upfile/";




/* APIName */
NSString *const APINameHomeAccount = @"appservice/";
NSString *const APINameApp = @"app/";



@interface HAPIManager()
@property (nonatomic, strong, readwrite)HOwner *owner;
@end

@implementation HAPIManager
@synthesize owner = _owner;
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static HAPIManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

#pragma mark - Post 文件请求方法

//登录接口
- (RACSignal *)userLoginWithAccount:(NSString *)account
                           password:(NSString *)password {
    NSMutableDictionary *paramater = [NSMutableDictionary dictionaryWithDictionary:@{@"user_code":account?:@"",
                                                                                     @"user_password":password?:@""}];
    return [self requestPostReturnJSONAndObjectWithPrivateParameter:paramater
                                                        resultClass:nil
                                                      apiModule:APIModuleHome
                                                        apiName:APINameHomeAccount
                                                            key:@"login"];

}


/* Post */
- (RACSignal *)requestPostReturnJSONAndObjectWithPrivateParameter:(NSDictionary *)privateParameter
                                                      resultClass:(Class)resultClass
                                                        apiModule:(NSString *)apiModule
                                                          apiName:(NSString *)apiName
                                                              key:(NSString *)key {
    
    NSMutableDictionary *parameter = [[NSMutableDictionary alloc]initWithCapacity:0];
    if (privateParameter) {
        [parameter addEntriesFromDictionary:privateParameter];
    }
    return [[[self rac_POST:F(@"%@%@%@%@", APIServerUrl,apiModule,apiName,key) parameters:parameter]catch:^RACSignal *(NSError *error){
        NSError *er = [[NSError alloc]initWithDomain:MAPIManagerErrorDomain code:MAPIErrorNetworkError userInfo:@{@"message":@"请检查网络连接"}];
        return [RACSignal error:er];
    }]flattenMap:^RACStream *(RACTuple *value) {
        id resultDic = [self parseResultWithValue:[value first]];
        if ([resultDic isKindOfClass:[NSError class]]) {
            return [RACSignal error:resultDic];
        }else {
            return [RACSignal return:[value first]];
        }
    }];
}



/* Get */
- (RACSignal *)requestGetReturnJSONAndObjectWithPrivateParameter:(NSString *)privateParameter
                                                     resultClass:(Class)resultClass
                                                       apiModule:(NSString *)apiModule
                                                         apiName:(NSString *)apiName
                                                             key:(NSString *)key {
    
    
    NSString *urlString = F(@"%@%@%@%@", APIServerUrl,apiModule,apiName,key);
    return [[[self rac_GET:urlString parameters:privateParameter]catch:^RACSignal *(NSError *error) {
        NSError *er = [[NSError alloc]initWithDomain:MAPIManagerErrorDomain code:MAPIErrorNetworkError userInfo:@{@"message":@"请检查网络连接"}];
        return [RACSignal error:er];
    }]flattenMap:^RACStream *(id value) {
        
        return [RACSignal return:[value first]];
    }];
}
-(void)setOwner:(HOwner *)owner {
    _owner = owner;
}

- (id)parseResultWithValue:(id)value {
    NSNumber *flag;
    NSInteger code;
    NSString *message;
    if ([value isKindOfClass:[NSDictionary class]]) {
        flag = value[@"flag"];
        if ([flag isEqualToNumber:[NSNumber numberWithInt:0]]) {
            code = [flag integerValue];
            message = value[@"msg"];
            NSError *error = [[NSError alloc]initWithDomain:@"ErrorDomain" code:code userInfo:@{@"message":message}];
            return error;
        }else {
            return value;
        }
    }
    return nil;
}

@end
